{ This implements the Base58 encoding how it is used in Bitcoin

  Copyright (C) 2011 Bernd Kreuss <prof7bit@googlemail.com>

  ***

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit bitcoin_base58;
{$mode objfpc}{$H+}

interface

uses
  openssl_bignum, bitcoin_crypto, bitcoin_common;

type
  TBitcoinAddressEncoded = array[1..34] of Char; // big endian
  TBitcoinAddressBinary = array[1..25] of Char;  // big endian

function Base58Encode(Bin: TBitcoinAddressBinary): TBitcoinAddressEncoded;
function Base58Decode(Enc: TBitcoinAddressEncoded): TBitcoinAddressBinary;
{ Create the Base58 encoded bitcoin address from X and Y of the public
  key. X and Y are big endian Base256 of exactly 32 bytes each}
function KeyToAddress(x, y: String): TBitcoinAddressEncoded;


implementation
const
  Alphabet : array [0..57] of Char = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';

function Base58Encode(Bin: TBitcoinAddressBinary): TBitcoinAddressEncoded;
var
  C58, C0, Num, Dv, Rem : TBigNum;
  I, J  : Integer;
  Temp  : Char;
begin
  C58.Init('3a'); // 58
  C0.Init('0');
  Dv.Init;
  Rem.Init;
  Num.Init(@Bin, 25);

  J := 1;
  while Num > C0 do begin
    Num.DivMod(C58, Dv, Rem);
    Num.Assign(Dv);
    Result[J] := Alphabet[Byte(Rem.ToDWord)];
    Inc(J);
  end;

  // each leading zero byte in the binary is kept as one base58 zero ('1')
  // This is a little bit bizarre since the more leading zeros it has
  // the shorter the base58 address will be but Satoshi decided to do it
  // that way instead of just leaving them completely away or at least
  // properly padding to constant size.
  for I := 1 to 25 do begin
    if Bin[I] = #0 then begin
      Result[J] := Alphabet[0];
      Inc(J);
    end
    else
      break;
  end;
  Dec(J);

  if J < 34 then
    FillChar(Result[J+1], 34-J, ' ');

  // now we reverse the result string to make it big endian
  ReverseBuffer(Result, J);

  C58.Done;
  C0.Done;
  Dv.Done;
  Rem.Done;
  Num.Done;
end;


function Base58Decode(Enc: TBitcoinAddressEncoded): TBitcoinAddressBinary;
begin

end;

function KeyToAddress(x,y: String): TBitcoinAddressEncoded;
var
  Key        : String;
  Hash256    : TSHA256Digest;
  Sum4       : TCheckSum;
  BinAddr25  : TBitcoinAddressBinary; // (1 byte #$00) + (20 byte hash) + (4 byte checksum)
begin
  Key := #$04 + x + y;
  Assert(Length(key) = 65);

  // the double-hash will give us the 20 bytes in the middle
  SHA256(Key[1], 65, Hash256[1]);
  RIPEMD160(Hash256[1], 32, BinAddr25[2]);

  // this is the first byte
  BinAddr25[1] := #$00;

  // the checksum is over the first 21 bytes
  // and then appended at the end
  Sum4 := CheckSum(BinAddr25[1], 21);
  move(Sum4, BinAddr25[22], 4);

  // now we have the 25 byte long value
  // that will be converted to Base5
  Result := Base58Encode(BinAddr25);
end;

end.

